<template>
  <div class="opra-log-container">
    <el-card class="box-card" shadow="hover">
      <div style="margin-bottom: 10px;">
        <el-select v-model="status" style="margin-right: 15px;">
          <el-option label="选择操作状态" :value="-1" />
          <el-option label="失败" :value="0" />
          <el-option label="成功" :value="1" />
        </el-select>
        <el-button type="primary" :icon="Search" plain @click="searchByStatus">搜索</el-button>
        <el-button type="warning" :icon="Refresh" plain @click="reset">重置</el-button>
      </div>
      <div style="margin-bottom: 10px;">
        <el-popconfirm title="是否确认删除选中?" @confirm="deleteSelector">
          <template #reference>
            <el-button type="danger" :icon="Delete" plain>删除选中</el-button>
          </template>
        </el-popconfirm>
        <el-popconfirm title="是否确认清空日志?" @confirm="deleteFull">
          <template #reference>
            <el-button type="danger" :icon="DeleteFilled" plain>清空</el-button>
          </template>
        </el-popconfirm>
      </div>
      <el-table
        v-loading="loading"
        element-loading-text="Loading..."
        :element-loading-spinner="svg"
        element-loading-svg-view-box="-10, -10, 50, 50"
        :header-cell-style="{textAlign:'center'}"
        :cell-style="{textAlign:'center'}"
        @selection-change="selectionChange"
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column fixed prop="operName" label="操作账号" width="100" />
        <el-table-column prop="methodDesc" label="操作描述" width="250">
          <template #default="scope">
            <el-popover effect="light" trigger="hover" placement="top" width="auto">
              <template #default>
                <el-descriptions title="请求信息" :column="1" size="small" border>
                  <el-descriptions-item label="方法调用">{{ scope.row.methodName }}</el-descriptions-item>
                  <el-descriptions-item label="请求方式">{{ scope.row.requestMethod }}</el-descriptions-item>
                  <el-descriptions-item label="请求Url">{{ scope.row.operUrl }}</el-descriptions-item>
                  <el-descriptions-item label="请求参数">{{ scope.row.operParam }}</el-descriptions-item>
                </el-descriptions>
              </template>
              <template #reference>
                {{ scope.row.methodDesc }}
              </template>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="operIp" label="操作者IP信息" width="150" />
        <el-table-column prop="operLocation" label="IP所在地理位置" width="200" />
        <el-table-column prop="status" label="操作状态" width="200">
          <template #default="scope">
            <el-popover effect="light" trigger="hover" placement="top" width="auto">
              <template #default>
                <el-descriptions title="响应信息" :column="1" size="small" border>
                  <el-descriptions-item label="响应结果">{{ scope.row.operResult }}</el-descriptions-item>
                  <el-descriptions-item label="错误信息">{{ scope.row.errorMsg }}</el-descriptions-item>
                </el-descriptions>
              </template>
              <template #reference>
                <el-tag type="success" v-if="scope.row.status == 1">操作成功</el-tag>
                <el-tag type="danger" v-else>操作错误</el-tag>
              </template>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="operTime" label="操作时间" width="150"/>
        <el-table-column fixed="right" label="操作" width="100">
          <template #default="scope">
            <el-popconfirm title="确定删除该行数据?" @confirm="deleteRow(scope.row)">
              <template #reference>
                <el-button
                  type="danger"
                  size="small"
                >
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        small
        background
        layout="prev, pager, next"
        :hide-on-single-page="true"
        :page-size="pagerInfo.pageSize"
        :total="pagerInfo.total"
        class="mt-4 pagination"
        @current-change="pageChange"
        @prev-click="pageChange"
        @next-click="pageChange"
      />
    </el-card>
  </div>
</template>

<script setup>
import { Search, Refresh, DeleteFilled, Delete } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { OperationLog, DeleteOperationLog } from '@/api/logs'
import { DateFormat } from '@/utils/CommonFun.js'
import { svg } from '@/datas/loading'
import { ref } from 'vue'

// 数据加载状态的遮蔽罩
const loading = ref(true)

// 表格信息
const status = ref(-1)
const tableData = ref([])
const hasSearch = ref(false) // 是否使用搜索
const getOperaLog = pageNum => {
  OperationLog({ pageSize: 10, pageNum, status: hasSearch.value ? status.value : -1 })
    .then(data => {
      pagerInfo.value.total = data.operationLogPage.total
      pagerInfo.value.pageSize = data.operationLogPage.size
      pagerInfo.value.currentPage = data.operationLogPage.current
      data.operationLogPage.records.forEach(item => {
        item.operTime = DateFormat.format(item.operTime)
      })
      tableData.value = data.operationLogPage.records
      loading.value = false
    })
}
getOperaLog(1)

// 分页器的数据及逻辑
const pagerInfo = ref({
  total: 1,
  pageSize: 10,
  currentPage: 1
})
const pageChange = (pageNum) => {
  loading.value = true
  getOperaLog(pageNum)
}

// 多选按钮的事件
const deleteRowIds = ref([])
const selectionChange = (selection) => {
  // 置空选项
  deleteRowIds.value = []
  // 将选择器中选中的id获取
  selection.forEach(item => {
    deleteRowIds.value.push(item.id)
  })
}
// 操作按钮的删除
const deleteRow = (row) => {
  DeleteOperationLog([row.id])
    .then(data => {
      pageChange(pagerInfo.value.currentPage)
    })
}
// 删除多选
const deleteSelector = () => {
  if (!deleteRowIds.value.length) {
    ElMessage({
      message: '没有选中需要删除的选项',
      type: 'warning'
    })
    return
  }
  DeleteOperationLog(deleteRowIds.value)
    .then(data => {
      pageChange(pagerInfo.value.currentPage)
    })
}
// 清空按钮
const deleteFull = () => {
  ElMessage({
    message: '清空按钮未被使用',
    type: 'info'
  })
}
// 搜索按钮
const searchByStatus = () => {
  hasSearch.value = true
  pageChange(1)
}
// 重置按钮
const reset = () => {
  hasSearch.value = false
  status.value = -1
  pageChange(1)
}
</script>

<style lang="scss" scoped>
.opra-log-container{
  .pagination{
    margin-top: 18px;
  }
}
</style>
